package view
{
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.*;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	
	public class Cart extends MovieClip
	{
		private var carSpeed:int = 3;
		//needed for calculate speed for shorter ways
		private var carSpeedReferenceWay = 465;	
		
		public function Cart(x:Number = -193.45, y:Number = 393.95)
		{
			this.x = x;
			this.y = y;
			this.stop();
		}
		
		public function moveCartFromCurrentXTo(
				newX:Number,
				finishEventListener:Function,
				turn:Boolean = false 
			):void {
			var speed = (carSpeed * Math.abs(this.x - newX)) / carSpeedReferenceWay; 
			var soundChannel:SoundChannel = new Sound(new URLRequest(Config.SqueakSound)).play();
			var tw:Tween = 	new Tween(this, "x", None.easeIn, this.x, newX, speed, true);
	
			tw.addEventListener(TweenEvent.MOTION_FINISH, finishEventListener);
			tw.addEventListener(
				TweenEvent.MOTION_FINISH, 
				function (e:Event) { soundChannel.stop()}
			);
			
			if (turn && this.currentFrame == 2) {
				this.gotoAndStop(1);
			} else if (turn && this.currentFrame == 1) {
				tw.addEventListener(
					TweenEvent.MOTION_FINISH,
					function (e:Event) {
						gotoAndStop(2);
					}	
				);
			}
		}	
		
		public function startFaceMoving(soundChannel:SoundChannel):void
		{
			this.face.startSpeakMoving(soundChannel);			
		}
		
		public function stopFaceMoving(e:Event):void
		{
			this.face.stopSpeakMoving(e);
		}
	}
}